package com.example.userclient.activity

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.widget.SearchView
import androidx.annotation.Nullable
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.baidu.mapapi.map.MapView
import com.example.userclient.Main.ClientInfoHolder
import com.example.userclient.QrCodeUtil
import com.example.userclient.R
import com.example.userclient.SaleBookListAdapter
import com.example.userclient.databinding.ActivityMainBinding


class MainActivity : AppCompatActivity() {


    private var mMapView: MapView? = null
    override fun onCreate(@Nullable savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val bind =  DataBindingUtil.setContentView<ActivityMainBinding>(
            this,
                R.layout.activity_main
        )


        if (ClientInfoHolder.locationDescribe!=null){
            bind.describe = ClientInfoHolder.locationDescribe
        }else{
            bind.describe = "点击获取当前位置"
        }
        bind.locText.isSelected =true
        bind.locImg.setOnClickListener{
            bind.describe = ClientInfoHolder.locationDescribe
            bind.locText.isSelected =true
        }


        val sharedPref: SharedPreferences = getSharedPreferences(
            "MyApplication",
            Context.MODE_PRIVATE
        )
        val defaultValue = sharedPref.getString("phone", null)
        if (defaultValue!=null){
            ClientInfoHolder.phone = defaultValue

            ClientInfoHolder.phone_bitmap = QrCodeUtil.createQRCodeBitmap(
                    ClientInfoHolder.phone,
                    200,
                    200,
                    "UTF-8",
                    "H",
                    "1",
                    Color.BLACK,
                    Color.WHITE
            );
            ClientInfoHolder.loadNetBook()
        }else{
//            FillPhoneActivity.actionStartFromOutSide(this)
            val intent = Intent(this, FillPhoneActivity::class.java)
            startActivity(intent);
            Log.i("MyApplication","FillPhoneActivity out ${ClientInfoHolder.phone}")
            ClientInfoHolder.phone_bitmap = QrCodeUtil.createQRCodeBitmap(
                    ClientInfoHolder.phone,
                    200,
                    200,
                    "UTF-8",
                    "H",
                    "1",
                    Color.BLACK,
                    Color.WHITE
            );
            ClientInfoHolder.loadNetBook()
        }
        bind.searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                SaleBookListAdapter.filterByQuery(query)
                SaleBookListAdapter.notifyDataSetChanged()

                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                Log.i("MainActivity", "$newText")
//                if (newText.isNullOrBlank()) {
//                    BookListAdapter.setDataSet(ClientInfoHolder.books!!)
//                    BookListAdapter.notifyDataSetChanged()
//                }
                return false;
            }
        })

        bind.searchView.setOnCloseListener{
            SaleBookListAdapter.clean()
            false
        }
    }


    override fun onResume() {
        super.onResume()
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
//        mMapView!!.onResume()
    }

    override fun onPause() {
        super.onPause()
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
//        mMapView!!.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
//        mMapView!!.onDestroy()
    }

    companion object{
        fun back(context: Context){
            val intent = Intent(context, MainActivity::class.java)
            context.startActivity(intent)

        }
    }
}